export default function generateModel(genList) {
  const models = {}, rules = {}
  for (let i = 0; i < genList.length; i++) {
    if (genList[i].type === 'grid') {
      genList[i].columns.forEach(item => {
        const ret = generateModel(item.list)
        Object.assign(models, ret.models)
        Object.assign(rules, ret.rules)
      })
    } else {
      if (genList[i].type === 'blank') {
        models[genList[i].model] = genList[i].options.defaultType === 'String' ? '' : (genList[i].options.defaultType === 'Object' ? {} : [])
      } else {
        models[genList[i].model] = genList[i].options.defaultValue
      }

      if (rules[genList[i].model]) {
        rules[genList[i].model] = [...rules[genList[i].model], ...genList[i].rules.map(item => {
          if (item.pattern) {
            return {...item, pattern: eval(item.pattern)}
          } else {
            return {...item}
          }
        })]
      } else {
        rules[genList[i].model] = [...genList[i].rules.map(item => {
          if (item.pattern) {
            return {...item, pattern: eval(item.pattern)}
          } else {
            return {...item}
          }
        })]
      }
    }
  }

  return {
    models,
    rules
  }
}
